<?php
class tag_replace_content extends action{
	// 行为扩展的执行入口必须是run
	public function run(&$content) { 
		$content = $this->_replace($content); 
	}
	private function _replace($content) {
    	$replace = array();$siteUrl = C('ik_site_url');
    	//网站地址 带 / 如：http://www.ikphp.com/
        $replace['__SITE_URL__']		= $siteUrl;
        //网站APP静态文件目录
        $replace['__APP_STATIC__'] 		= C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/css';
        //网站APP应用风格路径
        $replace['__STATIC_CSS__'] 		= C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/css';
        //网站APP应用风格图片路径
        $replace['__STATIC_IMG__'] 		= C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/images';
        //网站APP应用风格图片路径
        $replace['__STATIC_JS__']  		= C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/js';
        
        //网站基本风格
        $basecss = './public/theme/'.C('ik_site_theme').'/base.css'; 
        //APP风格默认样式
        $appcss = C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/css/style.css'; 
        //APP风格下的controll_css样式
        $app_controll_css = C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/css/'.strtolower(CONTROLLER_NAME).'.css';  
        
	    $sitecss = '';
        if(is_file($basecss)){
        	$sitecss = '<link rel="stylesheet" type="text/css" href="'.$basecss.'" id="baseTheme" />';
        } 

	    if(cookie('ikTheme')){
        	$iktheme  = 'public/theme/'.cookie('ikTheme').'/base.css';
        	$sitecss .= '<link rel="stylesheet" type="text/css" href="'.$iktheme.'" id="ikTheme" />';
        }else{
        	$sitecss .= '<link rel="stylesheet" type="text/css" href="" id="ikTheme" />';
        } 
        if(is_file($appcss)){
        	$sitecss .= '<link rel="stylesheet" type="text/css" href="'.$appcss.'" id="appTheme" />';
        }
        if(is_file($app_controll_css)){
        	$sitecss .= '<link rel="stylesheet" type="text/css" href="'.$app_controll_css.'" id="controllTheme" />';
        }   
         //开始替换css
        $replace['__SITE_THEME_CSS__'] = $sitecss;
	        
        //扩展js
        $appextendjs = C('DEFAULT_THEME_PATH').C('DEFAULT_THEME').'/'.MODULE_NAME.'/js/extend.func.js';
        if(is_file($appextendjs)){
        	$replace['__EXTENDS_JS__'] = '<script src="'.$appextendjs.'" type="text/javascript"></script>';
        }else{
        	$replace['__EXTENDS_JS__'] = '';
        }        

        $content = str_replace(array_keys($replace),array_values($replace),$content);
    	return $content;        
	}
}
